package someTestExcemple.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws IOException {
        //模拟服务器接收数据
        System.out.println("服务器启动-------");
        //构造数据报套接字并将其绑定到本地主机上的指定端口
        DatagramSocket socket = new DatagramSocket(10086);
        //创建数据包对象 用来接收数据
        byte[] buffer = new byte[1024*64]; //64kb --udp 最大64kb
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while (true) {
            //接收数据
            socket.receive(packet);
            //从字节数组中提取数据 读取多少就倒多少
            // 本次接收的数据长度
            int len = packet.getLength();
            String rs = new String(buffer, 0, len);
            System.out.println("服务端接受数据成功！~~~");
            System.out.println(rs);
            System.out.println("hostName:" + packet.getAddress().getHostName());
            System.out.println("hostAddress:" + packet.getAddress().getHostAddress() + "port" + packet.getPort());
            System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            //socket.close();
        }

    }
}
